package com.lh.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lh.commonutils.R;
import com.lh.service.VodService;

import com.lh.util.ConstanVodUtil;
import com.lh.util.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author YMS
 * @date 2021/6/8
 */
@RestController
@RequestMapping("service_vod")
@Api(value = "视频点播")
//@CrossOrigin
public class VodController {

    @Resource
    private VodService vodService;


    @PostMapping("uploadALYVideo")
    @ApiOperation(value = "上传视频到阿里云")
    public R uploadALYVideo(MultipartFile file){
      R r= vodService.uploadALYVideo(file);
        return r;
    }

    @DeleteMapping("removeALYVideo/{videoSourceId}")
    @ApiOperation(value = "根据阿里云视频id删除阿里云上的视频")
    public R removeALYVideo(@PathVariable String videoSourceId){
      R r=vodService.removeALYVideo(videoSourceId);
      return r;
    }

    @DeleteMapping("delete-batch")
    @ApiOperation(value = "根据阿里云视频id删除阿里云上的视频")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
      R r= vodService.deleteBatch(videoIdList);
        return R.ok();
    }

//    根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public  R getPlayAuth(@PathVariable String id) throws Exception{
        DefaultAcsClient client = InitVodClient.initVodClient(ConstanVodUtil.ACCESS_KEY_ID, ConstanVodUtil.ACCESS_KEY_SECRET);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

        request.setVideoId(id);
        response = client.getAcsResponse(request);
//        System.out.println(response.getPlayAuth());
        return R.ok().data("playAuth",response.getPlayAuth());
    }

}
